package com.ejoysoft.servlet;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.sql.SQLException;
import java.util.Vector;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.ejoysoft.common.Globa;
import com.ejoysoft.wlanyz.business.Ap;
import com.ejoysoft.wlanyz.business.Ac;
import com.ejoysoft.wlanyz.business.Comunity;
public class ReturnApInfoServlet extends HttpServlet implements Servlet
{
	
	private static final long serialVersionUID = 217251451801586160L;
	public ReturnApInfoServlet()
	{
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException {	// TODO Auto-generated method stub
		try {
			
		this.execute(req, resp);
		}catch(Exception e) {
			e.printStackTrace();
			throw new IOException(e.getMessage());
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		System.err.println("12");
		this.doGet(req, resp);
	}

	private void execute(HttpServletRequest req, HttpServletResponse resp) throws IOException
	{
		 Globa globa = new Globa(); 
		 String content1 =  req.getParameter("Mac");
		 String content2 =  req.getParameter("Lac");
		 String content3 =  req.getParameter("Ci");
		 String where=" where 1=1 ";
		 String where1=" where 1=1";
		 if(content1!=null)
		 {
			 where+=" and strMac_Addr = '"+content1+"'";
		 }
		 if(content2!=null)
		 {
			 where1+=" and strLac = '"+content2+"'";
		 }
		 if(content3!=null)
		 {
			 where1+=" and strCI = '"+content3+"'";
		 }
		 StringBuffer sbReturn = new StringBuffer("<?xml version='1.0' encoding='utf-8'?> ");
			req.setCharacterEncoding("utf-8");		
			resp.setCharacterEncoding("utf-8");	
			try
			{
				    Ap obj0 = new Ap(globa,false);
					Vector<Ap> vctAd = new Vector<Ap>();
					vctAd = obj0.list(where,0,0);
					sbReturn.append("<Root>");
					if (vctAd!=null && vctAd.size() > 0)
					{
						for (int i = 0; i < vctAd.size(); i++)
						{
							obj0 = vctAd.get(i);
							if (obj0!=null)
							{   
								sbReturn.append(returnApInfo(obj0));
							}
						}
					}
					else{
						Comunity objComunity= new Comunity(globa,false);
						Vector<Comunity> vctC = new Vector<Comunity>();
						vctC = objComunity.list(where1,0,0);
						if (vctC!=null &&vctC.size() > 0)
						{
							for( int i=0;i < vctC.size();i++)
							{
								objComunity= vctC.get(i);
								if(objComunity!=null)
								{
									sbReturn.append(returnApInfo1(objComunity));	
								}
							}
						}
						
					}
					
					sbReturn.append("</Root>");
				
			} catch (Exception e)
			{
				e.printStackTrace();
			} finally
			{
				// 关闭数据库连接对象
				globa.closeCon();
				//System.out.println(sbReturn.toString());
				resp.getWriter().println(sbReturn.toString());
			}
 
	  }
	


	private StringBuffer returnApInfo (Ap obj1 ) throws SQLException
	{   
		Globa globa = new Globa();
				StringBuffer sbReturnContent = new StringBuffer();
				if(obj1!=null)
				{
					sbReturnContent.append("<apinfo>");
					sbReturnContent.append("<ApName>" + obj1.getDevice_Name() + "</ApName>");
					sbReturnContent.append("<Mac>" + obj1.getStrMac_Addr() + "</Mac>");
					
					String hotpotid=obj1.getStrHotSpot();
					if(hotpotid!=null){
						 String Where= " where strId = '" + hotpotid + "'";
						Ac objAc=new Ac(globa);
						Ac obj2=objAc.show(Where);
						if(obj2!=null){
						sbReturnContent.append("<city>" +obj2.getStrCity()+"</city>");
						sbReturnContent.append("<town>"+obj2.getStrTown()+"</town>");	
						}
					}
					
					sbReturnContent.append("</apinfo>");	
				}
				globa.closeCon();
				return sbReturnContent;
			  }
				
				
	private StringBuffer returnApInfo1 (Comunity obj1 ) throws SQLException
	{   
		Globa globa = new Globa();
				StringBuffer sbReturnContent = new StringBuffer();
				if(obj1!=null)
				{
					sbReturnContent.append("<apinfo>");
					sbReturnContent.append("<Lac>" +obj1.getStrLac()+"</Lac>");
					sbReturnContent.append("<Ci>"+obj1.getStrCI()+"</Ci>");
					sbReturnContent.append("<city>" +obj1.getStrCity()+"</city>");
					sbReturnContent.append("<town>"+obj1.getStrTown()+"</town>");	
					sbReturnContent.append("</apinfo>");	
				}
				globa.closeCon();
				return sbReturnContent;
	}
				 
}
	
